﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GestionPedago.Services.ES.Modele;
using System.Collections.ObjectModel;
using GestionPedago.Services;
using GestionPedago.View;
using GestionPedago.Services.ES;
using System.ComponentModel;
using System.Windows.Data;

namespace GestionPedago.ViewModel {
    
    /// <summary>
    /// Classe qui représente l'onglet Fiche stagiaire
    /// </summary>
    public class FicheStagiaireVM : OngletVM {


        private ICommand createEventCommand;
        public ICommand CreateEventCommand 
        {
            get {
                if(this.createEventCommand == null)
                    this.createEventCommand = new RelayCommand(param => this.OpenCreateEvent());

                return this.createEventCommand;
            }
        }

        private void OpenCreateEvent() {
            CreaModEvenementPedagogiqueVM vmCreaModEvenementPedagogique = new CreaModEvenementPedagogiqueVM(Parent);
            Parent.Onglets.Add(vmCreaModEvenementPedagogique);
            Parent.SelectedOnglet = vmCreaModEvenementPedagogique;
        }



        private ICommand planifECFCommand;
        public ICommand PlanifECFCommand {
            get {
                if(this.planifECFCommand == null)
                    this.planifECFCommand = new RelayCommand(param => this.OpenPlanifStagiaire());

                return this.planifECFCommand;
            }
        }

        private void OpenPlanifStagiaire() {
            PlanifierECFStagiaireVM vmPlanifierECFStagiaire = new PlanifierECFStagiaireVM(Parent);
            Parent.Onglets.Add(vmPlanifierECFStagiaire);
            Parent.SelectedOnglet = vmPlanifierECFStagiaire;
        }


        private PlanningIndividuelFormationES pPlaningIndivFormation;
        /// <summary>
        /// Planing individuel de formation sélectionné
        /// </summary>
        public PlanningIndividuelFormationES PlaningIndivFormation {
            get { return pPlaningIndivFormation; }
            set {
                if(value != null) {
                    pPlaningIndivFormation = value;
                    Appreciations = new ObservableCollection<AppreciationES>(FacadeServices.GetInstance().RecupererAppreciations(new Services.ES.RecupererAppreciationsIn(PlaningIndivFormation)).ListeAppreciations);
                    RaisePropertyChanged("PlaningIndivFormation");
                }

            }
        }


        private ObservableCollection<AppreciationES> pAppreciations;
        public ObservableCollection<AppreciationES> Appreciations 
        {
            get { return pAppreciations; }
            set {
                if(value != null) {
                    pAppreciations = value;
                    RaisePropertyChanged("Appreciations");
                }

            }
        }


        private ObservableCollection<ResultatES> pListeResultats;
        public ObservableCollection<ResultatES> ListeResultats {
            get { return pListeResultats; }
            set {
                pListeResultats = value;
                RaisePropertyChanged("ListeResultats");
            }
        }


        private ICommand addAppreciationCommand;
        public ICommand AddAppreciationCommand {
            get {
                if(this.addAppreciationCommand == null)
                    this.addAppreciationCommand = new RelayCommand(param => this.AjouterAppreciation());

                return this.addAppreciationCommand;
            }
        }

        /// <summary>
        /// Créer une nouvelle activité
        /// </summary>
        private void AjouterAppreciation() {

            if(PlaningIndivFormation != null)
            {
                EditAppreciation addwindow = new EditAppreciation(Formateurs.ToList(), PlaningIndivFormation.ListePlanningIndividuelDetail);
                bool? result = addwindow.ShowDialog();
                if(result ?? false) {


                    AppreciationES a = new AppreciationES();
                    a.DateCreation = DateTime.Now;
                    a.ContactENI = Formateurs.Single(c => c.CodeContactENI == addwindow.Formateur.CodeContactENI);
                    a.CodeContactENI = addwindow.Formateur.CodeContactENI;
                    a.Cours = PlaningIndivFormation.ListePlanningIndividuelDetail.Single(c => c.Cours.IdCours == addwindow.Cours.IdCours).Cours;
                    a.IdCours = addwindow.Cours.IdCours;
                    a.Remarque = addwindow.Remarque;
                    a.PlanningIndividuelFormation = PlaningIndivFormation;
                    a.CodePlanning = PlaningIndivFormation.CodePlanning;
                    a = FacadeServices.GetInstance().EnregistrerAppreciation(new EnregistrerAppreciationIn(a)).Appreciation;
                    Appreciations.Add(a);

                }
            }

        }



        private ICommand updateAppreciationCommand;
        public ICommand UpdateAppreciationCommand {
            get {
                if(this.updateAppreciationCommand == null)
                    this.updateAppreciationCommand = new RelayCommand(param => this.UpdateAppreciation());

                return this.updateAppreciationCommand;
            }
        }


        private void UpdateAppreciation() {

            if(Appreciation != null) {

                EditAppreciation addwindow = new EditAppreciation(Formateurs.ToList(), PlaningIndivFormation.ListePlanningIndividuelDetail);
                addwindow.Formateur = Appreciation.ContactENI;
                addwindow.Cours = Appreciation.Cours;
                addwindow.Remarque = Appreciation.Remarque;

                bool? result = addwindow.ShowDialog();
                if(result ?? false) {

                    Appreciation.ContactENI = Formateurs.Single(c => c.CodeContactENI == addwindow.Formateur.CodeContactENI);
                    Appreciation.CodeContactENI = addwindow.Formateur.CodeContactENI;
                    Appreciation.Cours = PlaningIndivFormation.ListePlanningIndividuelDetail.Single(c => c.Cours.IdCours == addwindow.Cours.IdCours).Cours;
                    Appreciation.IdCours = addwindow.Cours.IdCours;
                    Appreciation.Remarque = addwindow.Remarque;
                    Appreciation.PlanningIndividuelFormation = PlaningIndivFormation;
                    Appreciation.CodePlanning = PlaningIndivFormation.CodePlanning;
                    Appreciation = FacadeServices.GetInstance().EnregistrerAppreciation(new EnregistrerAppreciationIn(Appreciation)).Appreciation;

                    Appreciations = new ObservableCollection<AppreciationES>(FacadeServices.GetInstance().RecupererAppreciations(new Services.ES.RecupererAppreciationsIn(PlaningIndivFormation)).ListeAppreciations);

                }

            }


        }




        private ICommand delAppreciationCommand;
        public ICommand DelAppreciationCommand {
            get {
                if(this.delAppreciationCommand == null)
                    this.delAppreciationCommand = new RelayCommand(param => this.SupprimerAppreciation());

                return this.delAppreciationCommand;
            }
        }

        /// <summary>
        /// Supprimé définitivement une activité.
        /// </summary>
        private void SupprimerAppreciation() {
            if(Appreciation != null) {
                Appreciation.PlanningIndividuelFormation = PlaningIndivFormation;
                Appreciation.CodePlanning = PlaningIndivFormation.CodePlanning;
                Appreciations.Remove(Appreciation);
                FacadeServices.GetInstance().SupprimerAppreciation(new SupprimerAppreciationIn(Appreciation));
                if(Appreciations.Count > 0)
                    Appreciation = Appreciations[0];
            }
        }



        private ObservableCollection<ContactENIES> pFormateurs;
        /// <summary>
        /// Liste des Formateurs affichés dans la liste déroulante
        /// </summary>
        public ObservableCollection<ContactENIES> Formateurs {
            get { return pFormateurs; }
            set {
                pFormateurs = value;
                RaisePropertyChanged("Formateurs");
            }
        }


        private AppreciationES pAppreciation;
        /// <summary>
        /// Compétence sélectionnée dans la liste des compétences non utilisées par l'activité sélectionnée
        /// </summary>
        public AppreciationES Appreciation {
            get { return pAppreciation; }
            set {
                if(value != null) {
                    pAppreciation = value;
                    RaisePropertyChanged("Appreciation");
                }

            }
        }


        private PlanningIndividuelFormationES pPlaningIndivFormation2;
        /// <summary>
        /// Planing individuel de formation sélectionné
        /// </summary>
        public PlanningIndividuelFormationES PlaningIndivFormation2 {
            get { return pPlaningIndivFormation2; }
            set {
                if(value != null) {
                    pPlaningIndivFormation2 = value;
                    ListeResultats = new ObservableCollection<ResultatES>(FacadeServices.GetInstance().AfficherResultats(new AfficherResultatsIn(PlaningIndivFormation2)).ListeResultats);
                    RaisePropertyChanged("PlaningIndivFormation2");
                }

            }
        }


        /// <summary>
        /// Constructeur de la classe FicheStagiaireVM
        /// </summary>
        /// <param name="parent">Gestionnaire d'onglets</param>
        public FicheStagiaireVM(GestionnaireOngletsVM parent) 
        {
            base.DisplayName = "Fiche : " + parent.vmMainWindow.CurrentStagiaire.Prenom + " " + parent.vmMainWindow.CurrentStagiaire.Nom;
            base.Parent = parent;
            Formateurs = new ObservableCollection<ContactENIES>(FacadeServices.GetInstance().RecupererFormateurs(new RecupererFormateursIn()).ListeFormateurs);
            
        }

    }

}
